#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''使用装饰器实现上下文管理器'''

from contextlib import contextmanager
@contextmanager
def ListTransaction(thelist):
    workingcopy = list(thelist)
    #yield传递的值（workingcopy）作为__enter__()返回值
    yield workingcopy     #可以把yield后的代码理解为__exit__()方法中的内容
    #只有没异常的时候才会执行后面的语句
    thelist[:] = workingcopy

item = [1,2,3]
try:
    with ListTransaction(item) as lt:
        lt.append(4)
        lt.append(5)
        raise RuntimeError("wo cuo le")
except:
    pass
print(item)